<html>
<head><meta charset="utf-8"><title>unification of const values · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html">unification of const values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177525581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177525581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177525581">(Oct 07 2019 at 14:42)</a>:</h4>
<p>Hey <span class="user-mention" data-user-id="124288">@oli</span>, <span class="user-mention" data-user-id="121053">@varkor</span> :</p>
<p>I am looking at the code that handles unification of const values, and I have a question. <a href="https://github.com/rust-lang/rust/blob/e3cb9ea15a2082f39d4d4f10a22e779701dd0d64/src/librustc/ty/relate.rs#L575-L582" target="_blank" title="https://github.com/rust-lang/rust/blob/e3cb9ea15a2082f39d4d4f10a22e779701dd0d64/src/librustc/ty/relate.rs#L575-L582">Currently it does this</a>:</p>
<div class="codehilite"><pre><span></span><span class="w">        </span><span class="p">(</span><span class="n">a_val</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ConstValue</span>::<span class="n">Scalar</span><span class="p">(</span><span class="n">Scalar</span>::<span class="n">Raw</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}),</span><span class="w"> </span><span class="n">b_val</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">ty</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">ty</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">a_val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b_val</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nb">Ok</span><span class="p">(</span><span class="n">tcx</span><span class="p">.</span><span class="n">mk_const</span><span class="p">(</span><span class="n">ty</span>::<span class="n">Const</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">val</span>: <span class="nc">a_val</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">ty</span>: <span class="nc">a</span><span class="p">.</span><span class="n">ty</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">}))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>This kind of surprises me. I would expect two scalars to be relatable, but this seems to "equate" a scalar with any other constant value. <em>Oh</em>, I see, it's relating <code>a_val == b_val</code></p>



<a name="177525649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177525649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177525649">(Oct 07 2019 at 14:43)</a>:</h4>
<p>I guess it would be equivalent to do something like this?</p>
<div class="codehilite"><pre><span></span><span class="p">(</span><span class="n">ConstValue</span>::<span class="n">Scalar</span><span class="p">(</span><span class="n">a_val</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="p">..),</span><span class="w"> </span><span class="p">(</span><span class="n">constValue</span>::<span class="n">Scalar</span><span class="p">(</span><span class="n">b_val</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="p">..)</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">ty</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">ty</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">a_val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b_val</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="177525810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177525810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177525810">(Oct 07 2019 at 14:45)</a>:</h4>
<p>I guess this FIXME answers my next question:</p>
<div class="codehilite"><pre><span></span><span class="w">        </span><span class="c1">// FIXME(const_generics): we should either handle `Scalar::Ptr` or add a comment</span>
<span class="w">        </span><span class="c1">// saying that we&#39;re not handling it intentionally.</span>
</pre></div>


<p>which was basically "what about Ptr?"</p>



<a name="177570385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177570385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177570385">(Oct 07 2019 at 23:20)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>: yes, that's right — what you've written is clearer</p>



<a name="177570393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177570393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177570393">(Oct 07 2019 at 23:20)</a>:</h4>
<p>see <a href="https://github.com/rust-lang/rust/pull/64986/files#diff-af1c8bc6ae3623b749b5aebeaa1e0bffR575-R578" target="_blank" title="https://github.com/rust-lang/rust/pull/64986/files#diff-af1c8bc6ae3623b749b5aebeaa1e0bffR575-R578">https://github.com/rust-lang/rust/pull/64986/files#diff-af1c8bc6ae3623b749b5aebeaa1e0bffR575-R578</a> also, which refactors that method a little bit</p>



<a name="177570417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/unification%20of%20const%20values/near/177570417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/unification.20of.20const.20values.html#177570417">(Oct 07 2019 at 23:21)</a>:</h4>
<p>it seems more readable there</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>